using System;
using EnvDTE;
using EnvDTE80;
using VSSDKAssist.Assets.ValueProviders.Base;

namespace VSSDKAssist.Recipes.Common.ValueProviders
{
    /// <summary>
    /// Value provider for a tt path absed on codeclass language
    /// </summary>
    public class TTPathValueProvider : ValueProviderBase
    {
        #region Constants
        private const string SubPathItemArgument = "SubPath";
        private const string TTFileArgument = "TTFile";
        #endregion

        #region Fields
        private string subPathItemArgument;
        private string ttFileArgument;
        #endregion

        /// <summary>
        /// Called when [begin recipe].
        /// </summary>
        /// <param name="currentValue">The current value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = null;

            if((currentValue == null) && (this.ExecutionStage != Stage.OnBeforeActions))
            {
                newValue = GetValue();

                return true;
            }

            return false;
        }

        /// <summary>
        /// Contains code that will be called before actions are executed.
        /// </summary>
        /// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
        /// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
        /// the new value of the argument, if the returned value
        /// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
        /// <returns>
        /// 	<see langword="true"/> if the argument value should be replaced with
        /// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>By default, always returns <see langword="false"/>, unless overridden by a derived class.</remarks>
        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            newValue = null;

            if((currentValue == null) && (this.ExecutionStage != Stage.OnBeginRecipe))
            {
                newValue = GetValue();

                return true;
            }

            return false;
        }

        private object GetValue()
        {
            string ttPath = string.Empty;

            if(VisualStudio.SelectedItems.Count == 1)
            {
                Project project = VisualStudio.SelectedItems.Item(1).Project as Project;

                if(project != null && project.CodeModel != null)
                {
                    string language;

                    if(project.CodeModel.Language.Equals(CodeModelLanguageConstants.vsCMLanguageCSharp))
                    {
                        language = "cs";
                    }
                    else if(project.CodeModel.Language.Equals(CodeModelLanguageConstants.vsCMLanguageVB))
                    {
                        language = "vb";
                    }
                    else if(project.CodeModel.Language.Equals(CodeModelLanguageConstants2.vsCMLanguageJSharp))
                    {
                        language = "js";
                    }
                    else
                    {
                        language = "cs";
                    }

                    ttPath = string.Format(@"TT\{0}\{1}\{2}", language, this.subPathItemArgument, this.ttFileArgument);

                    return ttPath;
                }
            }

            ttPath = string.Format(@"TT\{0}\{1}", this.subPathItemArgument, this.ttFileArgument);

            return ttPath;
        }

        /// <summary>
        /// Called when [configure].
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        protected override void OnConfigure(System.Collections.Specialized.StringDictionary attributes)
        {
            if(attributes == null)
                throw new ArgumentNullException("attributes");

            if(attributes.ContainsKey(SubPathItemArgument))
            {
                subPathItemArgument = attributes[SubPathItemArgument];
            }

            if(attributes.ContainsKey(TTFileArgument))
            {
                ttFileArgument = attributes[TTFileArgument];
            }
        }
    }
}